library(MASS) # Datasets library(mice) # Boys dataset library(dplyr) # Data manipulation library(magrittr) # Pipes library(ggplot2) # Plotting suite library(sf) # Spatial features
Statistical Programming in R
library(MASS) # Datasets library(mice) # Boys dataset library(dplyr) # Data manipulation library(magrittr) # Pipes library(ggplot2) # Plotting suite library(sf) # Spatial features
base graphics in Rggplot2 graphicsbase graphics in RFirst, recall:
height <- c(50.1, 53.5, 50.0, 54.5, 57.5) weight <- c(3.65, 3.37, 3.14, 4.27, 5.03)
boys <- boys head(boys)
## age hgt wgt bmi hc gen phb tv reg ## 3 0.035 50.1 3.650 14.54 33.7 <NA> <NA> NA south ## 4 0.038 53.5 3.370 11.77 35.0 <NA> <NA> NA south ## 18 0.057 50.0 3.140 12.56 35.2 <NA> <NA> NA south ## 23 0.060 54.5 4.270 14.37 36.7 <NA> <NA> NA south ## 28 0.062 57.5 5.030 15.21 37.3 <NA> <NA> NA south ## 36 0.068 55.5 4.655 15.11 37.0 <NA> <NA> NA south
To call a variable in the data frame, use the $ notation:
boys$hgt[1:10]
## [1] 50.1 53.5 50.0 54.5 57.5 55.5 52.5 53.0 55.1 54.5
plot(x = boys$hgt, y = boys$wgt, main = "Scatter plot",
xlab = "Height", ylab = "Weight", bty = "L")
plot(x = 1:5, y = exp(1:5), type = "l", main = "Line chart", bty = "L")
counts <- table(boys$reg) barplot(counts, main="Bar chart", ylab = "N")
counts <- table(boys$reg) pie(x=counts, main="Pie chart")
hist(boys$hgt, main = "Histogram", xlab = "Height")
boxplot(boys$hgt ~ boys$reg, main = "Boxplot",
xlab = "Region", ylab = "Height")
ggplot2?Layered plotting based on the book The Grammar of Graphics by Leland Wilkinsons.
With ggplot2 you
ggplot2 then takes care of the details
1: Provide the data
boys %>% ggplot()
2: map variable to aesthetics
boys %>% ggplot(aes(x = age, y = bmi))
3: state which geometric object to display
boys %>% ggplot(aes(x = age, y = bmi)) + geom_point()
Create the plot
gg <- boys %>% ggplot(aes(x = age, y = bmi)) + geom_point()
Add another layer (smooth fit line)
gg <- gg + geom_smooth(col = "dark blue")
Give it some labels and a nice look
gg <- gg + labs(x = "Age", y = "BMI", title = "BMI trend for boys") + theme_minimal()
plot(gg)
gg <-
boys %>%
filter(!is.na(reg)) %>%
ggplot(aes(x = age,
y = bmi,
size = hc,
colour = reg)) +
geom_point(alpha = 0.5) +
labs(title = "BMI trend for boys",
x = "Age",
y = "BMI",
size = "Head circumference",
colour = "Region") +
theme_minimal()
plot(gg)
geom_point
geom_bar
geom_line
geom_smooth
geom_histogram
geom_boxplot
geom_density
sf packagedata.framesWe have time for a cursory introduction at most.
denmark <- st_read("DK_map.shp")
plot(st_geometry(denmark))
denmark$proportion.over.70 <- denmark$over70/denmark$population
plot(denmark["proportion.over.70"],
main = "Proportion of population aged 70 years and above")
ggplotdenmark %>% ggplot(aes(fill=proportion.over.70)) + geom_sf()